import 'package:dio/dio.dart';
import 'http_manager.dart';
import 'package:universal_html/html.dart' as html;

class WanAndroidApi {
  static const String baseUrl = "http://www.wanandroid.com/";
  static const String articleList = "article/list/";
  static const String banner = "banner/json";

  static getArticleList(int page) async {
    return await HttpManager.getInstance(baseUrl).get('$articleList$page/json');
  }

  static getBanner() async {
    return await HttpManager.getInstance(baseUrl).get(banner);
  }
}

class PicsumPhotosApi {
  static const String baseUrl = "https://picsum.photos/";
  static const String listImages = "v2/list";

  static getImages(int page, {int limit = 30}) async {
    return await HttpManager.getInstance(baseUrl).get("$listImages?page=$page&limit=$limit");
  }
}

class SystemApi {
  static const String baseUrl = 'http://122.9.148.72:8081/';
  static const String downloadUrl = 'http://raxu5slq3.hn-bkt.clouddn.com/';

  static p_login(String pAccount, String pPassword) async {
    return await HttpManager.getInstance(baseUrl).post('p_login', body: {
      'pAccount' : pAccount,
      'pPassword' : pPassword,
    });
  }

  static p_register(String pAccount, String pPassword, String phone) async {
    return await HttpManager.getInstance(baseUrl).post('p_register', body: {
      'pAccount' : pAccount,
      'pPassword' : pPassword,
      'phone' : phone,
    });
  }

  static p_repassword(String cPassword, String cAccount) async {
    return await HttpManager.getInstance(baseUrl).post('p_repassword', body: {
      'cPassword' : cPassword,
      'cAccount' : cAccount,
    });
  }

  static see_person_infor(String pAccount) async {
    return await HttpManager.getInstance(baseUrl).get('see_person_infor', params: {
      'pAccount' : pAccount,
    });
  }

  static search_person(String pId) async {
    return await HttpManager.getInstance(baseUrl).get('search_person', params: {
      'pId' : pId,
    });
  }

  static see_person(String name) async {
    return await HttpManager.getInstance(baseUrl).get('see_person', params: {
      'name' : name,
    });
  }

  static enable_person(String pName, String phone, bool enable) async {
    return await HttpManager.getInstance(baseUrl).post('enable_person', body: {
      'pName' : pName,
      'phone' : phone,
      'enable' : enable,
    });
  }

  static c_login(String cAccount, String cPassword) async {
    return await HttpManager.getInstance(baseUrl).post('c_login', body: {
      'cAccount' : cAccount,
      'cPassword' : cPassword,
    });
  }

  static c_repassword(String cPassword, String cAccount) async {
    return await HttpManager.getInstance(baseUrl).post('c_repassword', body: {
      'cPassword' : cPassword,
      'cAccount' : cAccount,
    });
  }

  static c_register(
      String cAccount, String cPassword, String pId, String cName,
      String representative, String level, String nature, String industry,
      String client, html.File license, String address, String location,
      html.File consistent, html.File picture
  ) {
    html.FormData formData = html.FormData();
    formData.append('cAccount', cAccount);
    formData.append('cPassword', cPassword);
    formData.append('pId', pId);
    formData.append('cName', cName);
    formData.append('representative', representative);
    formData.append('level', level);
    formData.append('nature', nature);
    formData.append('industry', industry);
    formData.append('client', client);
    formData.appendBlob('license', license.slice());
    formData.append('address', address);
    formData.append('location', location);
    formData.appendBlob('consistent', consistent.slice());
    formData.appendBlob('picture', picture.slice());
    var request = html.HttpRequest();
    request.open('POST', baseUrl + 'c_register');
    request.send(formData);
    return request.onLoadEnd;
  }

  static see_company_infor(String pId) async {
    return await HttpManager.getInstance(baseUrl).get('see_company_infor', params: {
      'pId' : pId,
    });
  }

  static change_company(
      String PId, String representative, String level, String nature,
      String industry, String client, String location, String address, String remarks
  ) async {
    return await HttpManager.getInstance(baseUrl).post('change_company', body: {
      'PId' : PId,
      'representative' : representative,
      'level' : level,
      'nature' : nature,
      'industry' : industry,
      'client' : client,
      'location' : location,
      'address' : address,
      'remarks' : remarks,
    });
  }

  static consistent(String PId, String consistent) async {
    return await HttpManager.getInstance(baseUrl).post('consistent', body: {
      'PId' : PId,
      'consistent' : await MultipartFile.fromFile(consistent),
    });
  }

  static c_rename(String cAccount, String cName, String client, String file, String file2) async {
    return await HttpManager.getInstance(baseUrl).post('c_rename', body: {
      'cAccount' : cAccount,
      'cName' : cName,
      'client' : client,
      'file' : await MultipartFile.fromFile(file),
      'file' : await MultipartFile.fromFile(file2),
    });
  }

  static c_reaccount(String oAccount, String nAccount, String pId) async {
    return await HttpManager.getInstance(baseUrl).post('c_reaccount', body: {
      'oAccount' : oAccount,
      'nAccount' : nAccount,
      'pId' : pId,
    });
  }

  static rename_record(String pId) async {
    return await HttpManager.getInstance(baseUrl).get('rename_record', params: {
      'pId' : pId,
    });
  }

  static apply(String pAccount, String pName, String pId, String cName,
      String year, String date, String level, String file
  ) async {
    return await HttpManager.getInstance(baseUrl).post('apply', body: {
      'pAccount' : pAccount,
      'pName' : pName,
      'pId' : pId,
      'cName' : cName,
      'year' : year,
      'date' : date,
      'level' : level,
      'file' : await MultipartFile.fromFile(file),
    });
  }

  static person_record(String pAccount) async {
    return await HttpManager.getInstance(baseUrl).post('person_record', body: {
      'pAccount' : pAccount,
    });
  }

  static academic_infor(String pAccount, String graduationTime,
      String academicPrograms, String academicLevel, String academicNature,
      String school, String academicMajor, String academicDegree, String academicMark
  ) async {
    return await HttpManager.getInstance(baseUrl).post('academic_infor', body: {
      'pAccount' : pAccount,
      'graduationTime' : graduationTime,
      'academicPrograms' : academicPrograms,
      'academicLevel' : academicLevel,
      'academicNature' : academicNature,
      'school' : school,
      'academicMajor' : academicMajor,
      'academicDegree' : academicDegree,
      'academicMark' : int.parse(academicMark)
    });
  }

  static base_infor(String pAccount, int birth,
      String birthAddress, String phone, String archivesInstitution,
      String nation, String politicCountenance, String languageMsg,
      String specialtyMsg, String RPMsg, String scienceMsg
  ) async {
    return await HttpManager.getInstance(baseUrl).post('base_infor', body: {
      'pAccount' : pAccount,
      'birth' : birth,
      'birthAddress' : birthAddress,
      'phone' : phone,
      'archivesInstitution' : archivesInstitution,
      'nation' : nation,
      'politicCountenance' : politicCountenance,
      'languageMsg' : languageMsg,
      'specialtyMsg' : specialtyMsg,
      'RPMsg' : RPMsg,
      'scienceMsg' : scienceMsg
    });
  }

  static see_apply(String pAccount) async {
    return await HttpManager.getInstance(baseUrl).get('see_apply', params: {
      'pAccount' : pAccount
    });
  }

  static search_apply(String cName, {
    String? pName,
    String? pId,
    String? statue,
    String? level,
    int? year
  }) async {
    return await HttpManager.getInstance(baseUrl).get('search_apply', params: {
      'cName' : cName,
      if (pName != null)
        'pName' : pName,
      if (pId != null)
        'pId' : pId,
      if (statue != null)
        'statue' : statue,
      if (level != null)
        'level' : level,
      if (year != null)
        'year' : year
    });
  }

  static Future<Response> c_submmit_apply(int id, String companyResult) async {
    return HttpManager.getInstance(baseUrl).post('c_submmit_apply', body: {
      'id' : id,
      'companyResult' : companyResult,
    });
  }

  static Future<Response> r_submmit_apply(int id, String companyResult) async {
    return HttpManager.getInstance(baseUrl).post('r_submmit_apply', body: {
      'id' : id,
      'companyResult' : companyResult,
    });
  }
}